/**
 * 公共抽屉
 */
define([], function () {

    // 获取最顶层的window对象
    var twin = window;
    var doc = window.document;

    while (twin.parent && twin.parent !== twin) {
        try {
            // 跨域
            if (twin.parent.document.domain !== doc.domain) {
                break;
            }
        } catch (e) {
            break;
        }
        twin = twin.parent;
    }
    var body = document.body;

    // 生成唯一id
    function _getUUID(length) {
        return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36);
    }

    /**
     * 构造函数
     */
    function Drawer(options) {
        this.id = options.id || _getUUID(5);
        this._options = options;
        this.el = this.addDrawer();
        this.title = options.title || "";
        this.content = options.content || "";
        this._closed = options.onClosed;
        this.renderTitle();
        this.renderContent();
        this.initEvent();
    }

    // 生成dom
    Drawer.prototype.addDrawer = function () {
        var id = this.id;
        var $containerHtml = $("#drawer" + id);
        if ($containerHtml.length > 0) {
            return $containerHtml;
        } else {
            var options = this._options;
            var containerHtml = "" +
                "<div class=\"drawer drawer-right\" id=\"drawer" + id + "\" style=\"z-index: 1002\">\n" +
                "    <div class=\"drawer-mask\"></div>\n" +
                "    <div class=\"drawer-content clearfix\">\n" +
                "        <div class=\"drawer-header\"><span id='drawerTitle'></span><i class=\"aidicon aidicon-close drawer-close\"></i>\n" +
                "        </div>\n" +
                "        <div class=\"drawer-body\">\n" +
                "        </div>" +
                "        <div class=\"drawer-footer\"></div>\n" +
                "    </div>\n" +
                "</div>";

            $containerHtml = $(containerHtml);
            var button = addButton.call(this);
            if (button.length > 0) {
                $containerHtml.addClass("button-drawer");
            }
            button.forEach(function (item) {
                $containerHtml.find(".drawer-footer").append(item);
            });

            $(body).append($containerHtml);

            return $containerHtml;
        }

        // 添加底部按钮
        function addButton() {
            var that = this;
            var button2 = [];
            if (options.button && options.button.length > 0) {
                options.button.forEach(function (item) {
                    var intent = item.intent || "default";
                    var clickCb = item.click;
                    var buttonHtml = "<button class=\"btn btn-" + intent + "\">" + item.label + "</button>";
                    var $button = $(buttonHtml);
                    if (!clickCb) {
                        clickCb = function () {
                            // 关闭抽屉
                            that.close();
                        };
                    }
                    $button.click(function () {
                        clickCb();
                    });
                    button2.push($button);
                });
            }

            return button2;
        }
    };
    Drawer.prototype.open = function () {
        var that = this;
        // 禁用滚动条
        $("html").css({
            "overflow": "hidden"
        });
        this.el.show();
        // 设置异步执行改变抽屉的定位才能实现过渡效果
        setTimeout(function () {
            that.el.find(".drawer-content").css("transform", "none");
        });
    };

    Drawer.prototype.close = function () {
        var that = this;
        this.el.find(".drawer-content").css("transform", "translateX(100%)");
        // 等抽屉的过渡效果完成之后再消失（这里设置的时间应该与过渡动画的时间保持一致）
        setTimeout(function () {
            that.el.hide();
            // 解除滚动条禁用
            $("html").css({
                "overflow": "auto"
            });

            //如果有关闭回调则执行
            if (that._closed) {
                that._closed();
            }

        }, 300);
    };
    // 渲染内容
    Drawer.prototype.renderContent = function (content) {
        if (!content) {
            content = this.content;
        }

        this.el.find(".drawer-body").html(content);
        return this;
    };
    // 渲染内容和标题
    Drawer.prototype.renderOption = function (option) {
        if (option) {
            if (typeof option === "object") {
                if (option.content) {
                    this.content = option.content;
                }
                if (option.title) {
                    this.title = option.title;
                }
            }
        } else {
            return this;
        }

        this.renderContent();
        this.renderTitle();

        return this;
    };
    // 渲染标题
    Drawer.prototype.renderTitle = function () {
        this.el.find("#drawerTitle").text(this.title);
    };

    // 绑定事件
    Drawer.prototype.initEvent = function () {
        var that = this;

        this.el.find(".drawer-close").click(function () {
            that.close();
        });
    };
    return Drawer;
});
